package com.example.lin.ximalaya.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.example.lin.ximalaya.R;
import com.example.lin.ximalaya.adapter.viewholder.Type10or11ViewHolder;
import com.example.lin.ximalaya.adapter.viewholder.Type1ViewHolder;
import com.example.lin.ximalaya.adapter.viewholder.Type20or13ViewHolder;
import com.example.lin.ximalaya.adapter.viewholder.Type23ViewHolder;
import com.example.lin.ximalaya.adapter.viewholder.Type24ViewHolder;
import com.example.lin.ximalaya.adapter.viewholder.Type2ViewHolder;
import com.example.lin.ximalaya.adapter.viewholder.Type4ViewHolder;
import com.example.lin.ximalaya.adapter.viewholder.UnTypeViewHolder;
import com.example.lin.ximalaya.pojo.HotData;

/**
 * 推荐 适配器
 */
public class HotAdapter extends RecyclerView.Adapter {


    private Context context;
    private HotData hotData;

    public HotAdapter(Context context) {
        this.context = context;
    }

    public HotData getHotData() {
        return hotData;
    }

    /**
     * 关联适配器数据
     *
     * @param hotData
     */
    public void setHotData(HotData hotData) {
        this.hotData = hotData;
        notifyDataSetChanged();//刷新适配器
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //获取布局加载器
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        switch (viewType) {
            case TYPE_1:
                return new Type1ViewHolder(context, layoutInflater.inflate(R.layout.view_rv_item_type_1, parent, false));
            case TYPE_2:
                return new Type2ViewHolder(context, layoutInflater.inflate(R.layout.view_rv_item_type_2, parent, false));
            case TYPE_4:
                return new Type4ViewHolder(context, layoutInflater.inflate(R.layout.view_rv_item_type_4, parent, false));
            case TYPE_20:
            case TYPE_13:
                return new Type20or13ViewHolder(context, layoutInflater.inflate(R.layout.view_rv_item_type_20, parent, false));
            case TYPE_23:
                return new Type23ViewHolder(context, layoutInflater.inflate(R.layout.view_rv_item_type_23, parent, false));
            case TYPE_24:
                return new Type24ViewHolder(context, layoutInflater.inflate(R.layout.view_rv_item_type_24, parent, false));
            case TYPE_10:
            case TYPE_11:
                return new Type10or11ViewHolder(context, layoutInflater.inflate(R.layout.view_rv_item_type_10_or_11, parent, false));
        }
        //默认ViewHolder
        return new UnTypeViewHolder(layoutInflater.inflate(R.layout.view_rv_item_untype, parent, false));
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        HotData.ListBeanX listBeanX = hotData.getList().get(position);
        if (holder instanceof Type1ViewHolder) {
            Type1ViewHolder type1ViewHolder = (Type1ViewHolder) holder;
            type1ViewHolder.setData(listBeanX);
        } else if (holder instanceof Type2ViewHolder) {
            Type2ViewHolder type2ViewHolder = (Type2ViewHolder) holder;
            type2ViewHolder.setData(listBeanX);
        } else if (holder instanceof Type24ViewHolder) {
            Type24ViewHolder type24ViewHolder = (Type24ViewHolder) holder;
            type24ViewHolder.setData(listBeanX);
        } else if (holder instanceof Type4ViewHolder) {
            Type4ViewHolder type4ViewHolder = (Type4ViewHolder) holder;
            type4ViewHolder.setData(listBeanX);
        } else if (holder instanceof Type23ViewHolder) {
            Type23ViewHolder type23ViewHolder = (Type23ViewHolder) holder;
            type23ViewHolder.setData(listBeanX);
        } else if (holder instanceof Type20or13ViewHolder) {
            Type20or13ViewHolder type20ViewHolder = (Type20or13ViewHolder) holder;
            type20ViewHolder.setData(listBeanX);
        } else if (holder instanceof Type10or11ViewHolder) {
            Type10or11ViewHolder viewHolder = (Type10or11ViewHolder) holder;
            viewHolder.setData(listBeanX);
        }
    }

    private static final int TYPE_1 = 1;
    private static final int TYPE_2 = 2;
    private static final int TYPE_24 = 24;
    private static final int TYPE_10 = 10;
    private static final int TYPE_11 = 11;
    private static final int TYPE_15 = 15;
    private static final int TYPE_4 = 4;
    private static final int TYPE_23 = 23;
    private static final int TYPE_20 = 20;
    private static final int TYPE_13 = 13;

    /**
     * 确定 Item 的ViewHolder 类型
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        HotData.ListBeanX listBeanX = hotData.getList().get(position);
        //发现,喜马拉雅的App 是以 每个 Item的 moduleId 决定布局类型
        return listBeanX.getModuleId();
    }

    /**
     * 确定适配器的item的长度
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return hotData == null ? 0 : hotData.getList().size();
//        return hotData == null ? 0 : 8;
    }
}
